<?php
include_once('config/config.php');
if ($_POST) {
    $action = $_POST['action'];
    if ($action == 'checkImgcode') {
        $imgcode = $_POST['imgcode'];
        $sessionImgcode = strtolower($_SESSION['imgcode']);
        // pre($sessionImgcode);
        if ($imgcode != $sessionImgcode) {
            error('验证码错误');
        } else {
            success('验证码正确');
        }
    }
    if ($action == 'checkUsername') {
        $username = $_POST['username'];
        $admin = dbFind("SELECT * FROM `admin` WHERE `username` = '$username'");
        if (!$admin) {
            error('该用户不存在');
        } else {
            success('该用户存在');
        }
    }
    $username = $_POST['username'];
    $password = $_POST['password'];
    $admin = dbFind("SELECT * FROM `admin` WHERE `username` = '$username'");

    $repass = md5($password . $admin['salt']);
    if ($repass != $admin['password']) {
        ShowMsg('密码错误');
        exit;
    }

    $LoginAdmin = md5($admin['id'] . $admin['salt']);
    setcookie('LoginAdmin', $LoginAdmin);
    echo "<script>location.href = 'index.php';</script>";
    exit;
}

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>登录</title>
    <?php include_once('common/meta.php'); ?>
</head>

<body>

    <div class="navbar">
        <div class="navbar-inner">
            <a class="brand" href="index.html"><span class="second">后台管理</span></a>
        </div>
    </div>

    <div class="row-fluid">
        <div class="dialog">
            <div class="block">
                <p class="block-heading">登录</p>
                <div class="block-body">
                    <form method="post">
                        <label>用户名<span id="username" style="margin-left: 10px;"></span></label>
                        <input type="text" class="span12" placeholder="请输入用户名" name="username">
                        <label>密码<span id="password" style="margin-left: 10px;"></span></label>
                        <input type="password" class="span12" placeholder="请输入密码" name="password">
                        <label>验证码<span id="imgcode" style="margin-left: 10px;"></span></label>
                        <input type="text" class="span8" maxlength="4" placeholder="请输入验证码" name="imgcode">
                        <img style="margin-left: 10px;margin-top:-10px;" src="common/imgcode.php" onclick="this.src=`common/imgcode.php`" />
                        <button class="btn btn-primary  span6" style="margin-left: 90px;margin-top:10px;">登录</button>
                        <div class="clearfix"></div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>

</html>
<?php include_once('common/script.php'); ?>

<script>
    $('input[name="imgcode"]').change(function() {
        let imgcode = $(this).val();
        $.ajax({
            type: 'post',
            url: 'login.php',
            data: {
                imgcode,
                action: 'checkImgcode'
            },
            dataType: 'json',
            success: function(res) {
                if (res.code === 1) {
                    $('#imgcode').html(res.msg).css({
                        "color": "green"
                    })
                } else {
                    $('#imgcode').html(res.msg).css({
                        "color": "red"
                    })
                }
            },
            error: function(error) {
                console.log(error)
            }
        })
    });
    $('input[name="username"]').change(function() {
        let username = $(this).val();
        $.ajax({
            type: 'post',
            url: 'login.php',
            data: {
                username,
                action: 'checkUsername'
            },
            dataType: 'json',
            success: function(res) {
                if (res.code === 1) {
                    $('#username').html(res.msg).css({
                        "color": "green"
                    })
                } else {
                    $('#username').html(res.msg).css({
                        "color": "red"
                    })
                }
            },
            error: function(error) {
                console.log(error)
            }
        })
    });
</script>